<template>
  <div class="table-container">
    <el-card>
      <template #header>
        <div class="card-header">用户数据表格</div>
      </template>
      
      <el-row style="margin-bottom: 15px;">
        <el-col :span="8">
          <el-input 
            v-model="searchKey" 
            placeholder="输入用户名搜索" 
            suffix-icon="el-icon-search"
            @keyup.enter.native="fetchData">
            <el-button slot="append" @click="fetchData">搜索</el-button>
          </el-input>
        </el-col>
        <el-col :span="16" style="text-align: right;">
          <el-button type="primary" @click="handleCreate">新增用户</el-button>
          <!-- 修复：删除了多余的 </el-button> -->
        </el-col>
      </el-row>
      
      <el-table 
        :data="tableData" 
        stripe 
        border
        @row-click="handleRowClick">
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="username" label="用户名"></el-table-column>
        <el-table-column prop="gender" label="性别">
          <template slot-scope="scope">
            {{ scope.row.gender === 1 ? '男' : '女' }}
          </template>
        </el-table-column>
        <el-table-column prop="age" label="年龄"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="small" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      searchKey: ''
    }
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      console.log('请求参数:', {
        page: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.searchKey
      });
      
      this.$axios.get('http://127.0.0.1:8000/api/users/', {
        params: {
          page: this.currentPage - 1, // 后端页码从0开始
          pageSize: this.pageSize,
          keyword: this.searchKey
        }
      })
      .then(response => {
        console.log('接口响应:', response.data);
        
        this.tableData = response.data.items || [];
        this.total = response.data.total || 0;
        
        console.log('表格数据:', this.tableData);
        console.log('总记录数:', this.total);
      })
      .catch(error => {
        console.error('获取数据失败:', error);
        this.$message.error('获取数据失败');
      });
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.fetchData();
    },
    handleCurrentChange(newPage) {
      this.currentPage = newPage;
      this.fetchData();
    },
    handleCreate() {
      this.$message.info('创建用户功能待实现');
    },
    handleEdit(row) {
      this.$message.info(`编辑用户: ${row.username}`);
    },
    handleDelete(row) {
      this.$confirm('确定要删除此用户吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.delete(`/api/users/${row.id}`)
          .then(() => {
            this.$message.success('删除成功');
            this.fetchData();
          })
          .catch(() => {
            this.$message.error('删除失败');
          });
      }).catch(() => {
        // 取消操作
      });
    },
    handleRowClick(row) {
      this.$message.info(`点击了用户: ${row.username}`);
    }
  }
}
</script>

<style scoped>
.table-container {
  padding: 20px;
}

.card-header {
  font-size: 18px;
  font-weight: bold;
}
</style>